package com.xiaolin.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/9/28
 * @Version 1.0
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello(HttpServletRequest request, HttpServletResponse response){
        requestDemo(request);
        responseDemo(response);
        return "hello";
    }

    public static void requestDemo(HttpServletRequest request){
        //获取session对象
        HttpSession session = request.getSession();
        //获取请求参数
        String aa = request.getParameter("aa");
        try {
            //请求参数的默认编码是iso8859-1编码，可以设置成其他编码类型，如utf-8
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //设置属性
        request.setAttribute("bb",123);
        //获取请求头的属性值
        String cookie = request.getHeader("Cookie");
        String accept = request.getHeader("Accept");
        //获取属性值
        Integer bb = (Integer) request.getAttribute("bb");
        //获取 Servlet 上下文，这个对象是全局唯一的，一个服务只有一个该对象
        ServletContext servletContext = request.getServletContext();
        //获取上下文路径
        String contextPath = request.getContextPath();
        //获取请求方法类型
        String method = request.getMethod();
        //获取 servlet 的认证方案名，如果没有则返回null
        String authType = request.getAuthType();
        //获取所有的cookie
        Cookie[] cookies = request.getCookies();
        //获取所有请求头名
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            System.out.println(headerNames.nextElement());
        }
        //获取多个同名请求头对应的一组value值
        Enumeration<String> cs = request.getHeaders("Cookie");
        while (cs.hasMoreElements()){
            System.out.println(cs.nextElement());
        }
        //获取请求URI，这里是/app/hello，也就是除了请求协议、项目ip、端口和请求参数的部分
        String requestURI = request.getRequestURI();
        //获取请求URL，这里是 http://localhost:8060/app/hello，也就是除了请求参数部分
        StringBuffer requestURL = request.getRequestURL();
        //获取远程请求用户
        String remoteUser = request.getRemoteUser();
        //获取 servlet 请求路径，即 /hello
        String servletPath = request.getServletPath();
        //获取请求参数字符串
        String queryString = request.getQueryString();
        //获取 sessionId ，即JSESSIONID的值
        String requestedSessionId = request.getRequestedSessionId();
        //获取contentType，get请求没有contentType，post请求的contentType有多种类型
        String contentType = request.getContentType();
        //获取所有属性名
        Enumeration<String> attributeNames = request.getAttributeNames();
        //获取请求参数编码类型
        String characterEncoding = request.getCharacterEncoding();
        //获取请求体的长度
        int contentLength = request.getContentLength();
        try {
            //获取输入流，上传文件时使用
            ServletInputStream inputStream = request.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取本地ip地址，0:0:0:0:0:0:0:1是ipv6的表现形式，对应ipv4来说相当于127.0.0.1
        String localAddr = request.getLocalAddr();
        // 获取本地使用的语言集合
        Enumeration<Locale> locales = request.getLocales();
        //获取本地名称
        String localName = request.getLocalName();
        // 获取本地服务端口
        int localPort = request.getLocalPort();
        //获取请求参数集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //获取所有请求参数名
        Enumeration<String> parameterNames = request.getParameterNames();
        //获取请求参数数组
        String[] values = request.getParameterValues("aa");
        //获取服务名
        String serverName = request.getServerName();
        //获取服务端口
        int serverPort = request.getServerPort();
        //远程请求端口
        int remotePort = request.getRemotePort();
    }

    public static void responseDemo(HttpServletResponse response){
        Cookie cookie=new Cookie("hahha","123");
        response.addCookie(cookie);
        try {
            response.sendRedirect("http://localhost:8060/app/hi.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置ContentType
        response.setContentType("");
        //设置请求头
        response.setHeader("aa","123");
        //设置响应状态码
        response.setStatus(302);
//        try {
//            ServletOutputStream outputStream = response.getOutputStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        //设置响应内容的编码类型
        response.setCharacterEncoding("utf-8");
        try {
            //获取打印流，向浏览器页面打印数据（是向原网页打印数据，而不是重定向的页面）
            PrintWriter writer = response.getWriter();
            writer.write("hello");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
